namespace FIREFLYX {
    /**
     * 相机交互查看模型
     */
    export class CamViewModel implements IExample {
        public Init(canvas: HTMLCanvasElement) {
            const gl = canvas.getContext("webgl2");
            //app init
            App.init(gl.canvas as any);
            //简单观察相机
            const cVEnv = GenCamViewEnv();
            const cam = cVEnv.camera;
            const camHover = cVEnv.camHover;

            //光斑大小（大于0）
            const spotSpec = 100;
            //texture
            let tex = DefaultAsset.GetTexture(DefTexture.GRID);
            //create mesh
            let mesh = DefaultAsset.GetMesh(DefMesh.SPHERE);

            //引擎内建 shader
            const bSh = DefaultAsset.GetShader(FIREFLYX.BUILTIN_SHADER_BUMPED_SPECULAR);

            //模型节点
            const model = new GameObject("model");
            model.transform.scale = new Vector3(1.5, 1, 2);
            // model.transform.scale = new Vector3(2, 2, 2);
            model.transform.position = new Vector3(1, 0, 1);
            // FIREFLYX.Quaternion.RotationAlphaBetaGammaToRef(0,0.2,0,entity.transform.rotation);
            //模型渲染组件
            const mr = model.addComponent<MeshRenderer>(MeshRenderer);
            const baseMtr = new Material();
            mr.materialGroup[0] = baseMtr;
            baseMtr.shader = bSh;
            mr.mesh = mesh;

            //点状方向光源
            const light = GenMainLight();

            //材质设置
            baseMtr.setProperty(`u_mainColor`, new Color4(1, 0, 0, 1));
            baseMtr.setProperty(`ubo_material_block.u_spotSpec`, spotSpec);
            baseMtr.setProperty(`u_mainTex`, tex);

            // let angles = new Vector3(0,0,90 * Math.PI / 180);
            let angles = new Vector3();
            let speed = 0.1;

            GlobalEventManager.Instance.addListener("sceneUpdate", (dt: number) => {
                //旋转
                Quaternion.RotationYawPitchRollToRef(angles.y, angles.x, angles.z, model.transform.rotation);
                model.transform.rotation = model.transform.rotation;
            }, this);

            //IO Event
            //键盘控制旋转
            GlobalEventManager.Instance.addListener("keydown", (ev: KeyboardEvent) => {
                console.log(`onkeydown : ${ev.key} `);
                let dt = 1 * speed;
                switch (ev.key) {
                    case "a": angles.x += dt; break;
                    case "w": angles.y += dt; break;
                    case "s": angles.z += dt; break;
                    default:
                }
            }, this);
        }
    }
}